var api = require('../../utils/api.js');
var app = getApp();
import jiuaiDebounce from '../../utils/jiuai-debounce.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //active:0
    TabCur: 0,
    scrollLeft: 0,
    tabList: ['全部', '待付款', '待发货', '待收货', '已完成','退款/售后'],
    goodsList:[],
    pageNum:'',
    keywords:''
  },
  //搜索
  searchGoods(e) {
    var keywords = e.detail.value
    this.setData({
      keywords
    })
  },
  search1() {
    this.getOrderList(1, this.data.TabCur, this.data.keywords);
  },

  //切换tab
  tabSelect(e) {
    if (e.currentTarget.dataset.idx == this.data.TabCur) { return }
    this.setData({
      TabCur: e.currentTarget.dataset.idx,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 45,
    })
    this.getOrderList(1, this.data.TabCur, this.data.keywords);
  },

//跳转订单详情页面
  getLogistics(e){
    let b_id=e.currentTarget.dataset.bid
    wx.navigateTo({
      url: '/pages/logistics/logistics?b_id=' + b_id,
    })
  },

  //获取订单列表
  getOrderList(page, status, search){
    let params={
      token: wx.getStorageSync('token'),
      page,
      status,
      search
    }
    api.fetchApi('/order/my_order', params).then(res=>{
      console.log(res)
      var goodsList = this.data.goodsList;
      var goodsList1 = res.list;
      if (page == 1) {
        goodsList = res.list;
      } else {
        goodsList = [...goodsList, ...goodsList1]
      }
      this.setData({
        goodsList,
        page,
        pageNum: goodsList1.length,
        no_pay: res.no_pay,
        no_receipt: res.no_receipt,
        no_ship: res.no_ship,
        is_no: res.is_no
      })
    })
  },
  
  //跳转店铺列表
  toShopList(e){
    let store_id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shopList/shopList?store_id=' + store_id,
    })
  },
  // 跳转代理订单页面
  goAgentOrder(){
    wx.navigateTo({
      url: '/pages/agentOrder/agentOrder',
    })
  },
  //查看物流
  viewLogistics(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/expressInfo/expressInfo?bid='+id,
    })
  },

  //删除订单
  deleteOrder(e){
    let id=e.currentTarget.dataset.id;
    api.fetchApi('/order/del_order', {token: wx.getStorageSync('token'),id}).then(res=>{
      wx.showToast({
        title: '删除成功'
      })
      setTimeout(()=>{
        this.getOrderList(1, this.data.TabCur, '');
      },1000)
    })
  },

  //确认收货
  confirmReceipt(e){
    let id = e.currentTarget.dataset.id;
    api.fetchApi('/order/confirm_receipt', { token: wx.getStorageSync('token'), id }).then(res=>{
      wx.showToast({
        title: '确认成功'
      })
      setTimeout(()=>{
        this.getOrderList(1, this.data.TabCur, '');
      },1000)
    })
  },

  //立即支付
  payImmediately(e){
    jiuaiDebounce.canDoFunction({
      key: "tap",//基于此值判断是否可以操作，如两个方法传入了同样的key，则会混淆，建议传入调用此事件的方法名，简单好梳理
      time: 5000,//如果传入time字段，则为定时器后，自动解除锁定状态，单位（毫秒）
      success: () => {//成功中调用应该操作的方法，被锁定状态不会执行此代码块内的方法
        wx.showLoading({
          mask: true
        })

        let b_id = e.currentTarget.dataset.bid;
        console.log(b_id)

        let params = {
          token: wx.getStorageSync('token'),
          b_id
        }
        api.fetchApi('/Payment/once_pay_order', params).then(res => {
          let { nonceStr, package: package1, paySign, signType, timeStamp } = res;
          wx.requestPayment({
            timeStamp,
            nonceStr,
            package: package1,
            signType,
            paySign,
            success: (req) => {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(() => {
                this.getOrderList(1, 0, '');
              }, 500)
            },
            fail: (err) => {
              wx.showToast({
                title: '支付失败',
                icon: 'none'
              })

            }
          })
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pageUrl=options.page || ''
    this.setData({
      pageUrl
    })

    this.getOrderList(1,0,'');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.pageUrl == 'FillOrder'){
      wx.switchTab({
        url: '/pages/personal/personal',
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;
    if (this.data.pageNum < 5) {
      wx.showToast({
        title: '暂无更多',
        icon: 'none'
      })
      return
    }
    this.getOrderList(page, this.data.TabCur, this.data.keywords);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})